﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using CE.Provider;
using CE.Global;

public partial class provider_CarInsurance_ProviderCarCNG : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCICNG";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

    
    }

    protected void ibtSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {
                CarCNG _CarCNG = new CarCNG();
                CarCNGDB _CarCNGdb = new CarCNGDB();

                _CarCNG.Providercarcngid = CheckProviderCarCNG(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Boolean.Parse(ddlCNG.Text));
                int _trantype = 1;
                if (_CarCNG.Providercarcngid > 0)
                {
                    _trantype = 2;
                }
                _CarCNG.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
                _CarCNG.ProviderCompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());

                _CarCNG.CNG = bool.Parse(ddlCNG.Text);
                _CarCNG.Rate = Convert.ToSingle(txtRate.Text);
                _CarCNG.Status = true;
                _CarCNG.FInfo1 = 0;
                _CarCNG.DInfo1 = CE.Global.General.GetUTCDateTime();

                _CarCNG.Providercarcngid = _CarCNGdb.Insert(_CarCNG);
                if (_CarCNG.Providercarcngid > 0)
                {
                    InsertProviderAuthTransaction(_CarCNG.Providercarcngid, "CICarCNG", _trantype, 100, _authcode, _CarCNG.ProviderCompanyId, _CarCNG.RefCEUserId);
                    lblMessage.Text = "Updated successfully.";
                }
                else
                {
                    lblMessage.Text = "Updating failed.";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

 
    protected Int64 CheckProviderCarCNG(Int64 ProviderCompanyId, Boolean CNG)
    {
        CarCNG _CarCNG = new CarCNG();
        CarCNGDB _CarCNGdb = new CarCNGDB();
        Int64 _retValue = 0;
        _CarCNG = _CarCNGdb.GetProviderCarCNG(ProviderCompanyId, CNG);
        if (_CarCNG != null)
        {
            _retValue = _CarCNG.Providercarcngid;
        }
        return _retValue;
    }

    protected void ShowCarCNG(Int64 ProviderCompanyId, Boolean CNG)
    {
        CarCNG _CarCNG = new CarCNG();
        CarCNGDB _CarCNGdb = new CarCNGDB();

        _CarCNG = _CarCNGdb.GetProviderCarCNG(ProviderCompanyId, CNG);
        if (_CarCNG != null)
        {
            ddlCNG.SelectedValue = CNG.ToString();
            txtRate.Text = _CarCNG.Rate.ToString();
        }
        else
        {
            txtRate.Text = "";
        }
        lblMessage.Text = "";
    }

    protected void ddlCNG_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowCarCNG(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Boolean.Parse(ddlCNG.Text));
    }


    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/Provider/CarInsurance/TableMatching.aspx");
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?ut=300&mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?ut=300&mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
        }
    }
}
